package cn.edu.hunau.basic.day04;

import org.junit.Test;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @author hb
 * @date 2025-07-31 15:50:47
 */
public class ArrayCopyTest {

    @Test
    public void test04(){
        int[] src = {1,2,3,4,5};
        int[] result = Arrays.copyOf(src, 6);
        System.out.println(Arrays.toString(result));//[1, 2, 3, 4, 5, 0]
        int[] result2 = Arrays.copyOf(src, 3);
        System.out.println(Arrays.toString(result2)); //[1, 2, 3]
    }

    @Test
    public void test03(){
        Scanner sc=new Scanner(System.in);
        int [] arr=new int[10];
        int count =0;
        while(true){
            int num=sc.nextInt();
            if(num<=0){
                break;
            }
            if(count>=arr.length){
                //容量不够
                int[] temp=new int[arr.length*2];
                System.arraycopy(arr,0,temp,0,arr.length);
                arr=temp;
            }
            arr[count]=num;
            count++;
        }

        System.out.println("有效数据个数:"+count);
        int[] result =new int[count];
        System.arraycopy(arr,0,result,0,count);
        System.out.println(Arrays.toString(result));
    }


    @Test
    public void test02(){
        Scanner sc=new Scanner(System.in);
        int [] arr=new int[0];

        while(true){
            int num=sc.nextInt();
            if(num<=0){
                break;
            }
            //扩容
            int[] temp=new int[arr.length+1];
            System.arraycopy(arr,0,temp,0,arr.length);
            arr=temp;
            arr[arr.length-1] =num;
        }
        System.out.println(Arrays.toString(arr));
    }



    @Test
    public void testSystem(){
        int[] src = {1,2,3,4,5};
        int[] target = new int[5];//0,0,1,2,3
        System.arraycopy(src,0,target,2,3);
        System.out.println(Arrays.toString(target));
    }


}
